package com.yc.cloud.admin.controller;

import com.yc.cloud.common.basic.api.ResponseResult;
import com.yc.cloud.common.orm.CommonPage;
import com.yc.cloud.common.basic.dto.BasePageReqDTO;
import com.yc.cloud.admin.dto.request.AlgStrategyCreateReqDTO;
import com.yc.cloud.admin.dto.request.AlgStrategyUpdateReqDTO;
import com.yc.cloud.admin.dto.request.AlgStrategyApplyReqDTO;
import com.yc.cloud.admin.vo.AlgStrategyVo;
import com.yc.cloud.admin.service.AlgStrategyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;

/**
 * <p>
 * 算法策略表 前端控制器
 * </p>
 *
 * @author kinggu
 * @since 2025-07-15
 */
@RestController
@Api(tags = "算法策略表")
@RequestMapping("/alg-strategies")
public class AlgStrategyController {

    @Resource
    private AlgStrategyService algStrategyService;

    @ApiOperation("创建算法策略表")
    @PostMapping
    public ResponseResult<Boolean> create(@Validated @RequestBody AlgStrategyCreateReqDTO createReqDTO) {
        return ResponseResult.success(algStrategyService.create(createReqDTO));
    }

    @ApiOperation("更新算法策略表")
    @PutMapping("/{id}")
    public ResponseResult<Boolean> update(@PathVariable("id") Long id, @Validated @RequestBody AlgStrategyUpdateReqDTO updateReqDTO) {
        updateReqDTO.setId(id);
        return ResponseResult.success(algStrategyService.update(updateReqDTO));
    }

    @ApiOperation("应用策略到设备或分组")
    @PutMapping("/apply")
    public ResponseResult<Boolean> apply(@Validated @RequestBody AlgStrategyApplyReqDTO applyReqDTO) {
        return ResponseResult.success(algStrategyService.apply(applyReqDTO));
    }

    @ApiOperation("发布策略")
    @PutMapping("/{id}/publish")
    public ResponseResult<Boolean> publish(@PathVariable("id") Long id) {
        boolean result = algStrategyService.publish(id);
        return result ? ResponseResult.success(true, "策略发布成功") : ResponseResult.failed("策略发布失败");
    }

    @ApiOperation("取消发布策略")
    @PutMapping("/{id}/unpublish")
    public ResponseResult<Boolean> unpublish(@PathVariable("id") Long id) {
        boolean result = algStrategyService.unpublish(id);
        return result ? ResponseResult.success(true, "策略取消发布成功") : ResponseResult.failed("策略取消发布失败");
    }

    @ApiOperation("删除算法策略表")
    @DeleteMapping("/{id}")
    public ResponseResult<Boolean> delete(@PathVariable("id") Long id) {
        return ResponseResult.success(algStrategyService.removeById(id));
    }

    @ApiOperation("获取算法策略表详情")
    @GetMapping("/{id}")
    public ResponseResult<AlgStrategyVo> getById(@PathVariable("id") Long id) {
        return ResponseResult.success(algStrategyService.getVOById(id));
    }

    @ApiOperation("分页查询算法策略表")
    @GetMapping
    public ResponseResult<CommonPage<AlgStrategyVo>> list(@Validated BasePageReqDTO pageReqDTO) {
        return ResponseResult.success(algStrategyService.list(pageReqDTO));
    }
}
 